//
//  KLInformationViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/25.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLInformationViewController.h"
#import "KLInformationTableViewCell.h"
#import "STWebViewController.h"
@interface KLInformationViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation KLInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"公告";
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendInfomationRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无资讯" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
    }];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLInformationTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLInformationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KLInfoModel *  model = self.dataSouce[indexPath.row];
    STWebViewController * vc = [[STWebViewController alloc] initWithUrl:model.url];
    vc.forceTitle = model.post_title;
    vc.progressBackgroundColor = [UIColor whiteColor];
    vc.progressTintColor = KL_BlueBackGroundColor;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)afnteTest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
   // NSDictionary * test = @{@"name":@"sxl",@"age":@"23"};
    NSString * test = @[@{@"12":@"1,2,3"},
                       @{@"13":@"1,2,3,4,6,7,1"},
                       @{@"14":@"1,2,3,5,6,7,57"}
                        ];
    
    NSString * jsonStr = test.mj_JSONString;
    [dic setObject:jsonStr forKey:@"json"];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/text"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary *data = responseObject[@"data"];
                                                 [self showAlert:data.mj_JSONString andWithBlock:nil];
                                                 [SVProgressHUD showSuccessWithStatus:@"成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                  [SVProgressHUD dismiss];
                                                  [SVProgressHUD showErrorWithStatus:error.desc];

                                                
                                             }];
}
- (void)sendInfomationRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/consultation"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 self.dataSouce = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * array = dataDic[@"list"];
                                                     for (NSDictionary * modelDic in array) {
                                                         KLInfoModel * model = [KLInfoModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
@end
